﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SAPInvoker.Reflector;

namespace SAPInvoker.Controls
{
    public partial class NormalRFCParameterInput : UserControl
    {
        private RFCParameter _parameter;
        private Direction _direction;
        public NormalRFCParameterInput()
        {
            InitializeComponent();
        }

        public RFCParameter Parameter
        {
            get
            {
                this._parameter.Value = this.txtValue.Text;
                return this._parameter;
            }
            set
            {
                this._parameter = value;
                if (value != null)
                {
                    this.lblParamName.Text = value.Name;
                    this.lblTypeName.Text = value.Type.ToString();
                    this.lblPosition.Text = value.Position.ToString();
                    if (value.Value != null)
                    {
                        this.txtValue.Text = value.Value.ToString();
                    }
                    if (value.IsOptional)
                    {
                        this.chkOptional.Checked = true;
                    }
                }
            }
        }

        public Direction Direction
        {
            get { return this._direction; }
            set { this._direction = value; }
        }

        public string ToSql()
        {
            if (this.chkOptional.Checked && string.IsNullOrEmpty(this.txtValue.Text.Trim()))
            {
                return "";
            }
            else
            {
                if (this._direction == Direction.INPUT)
                {
                    return string.Format("@{0}='{1}'", this.lblParamName.Text, this.txtValue.Text);
                }
                else
                {
                    return string.Format("@{0}='{1}' {2}", this.lblParamName.Text, this.txtValue.Text, this._direction.ToString());
                }
            }
        }
    }

    public enum Direction
    {
        INPUT,
        OUTPUT,
        INPUTOUTPUT
    }
}
